import Taro from '@tarojs/taro';

/**
 * 提示与加载工具类
 */
export default class Tips {
  /**
   * 弹出提示框
   */

  static success(title: string, duration = 1000) {
    Taro.showToast({
      title: title,
      icon: 'success',
      mask: true,
      duration: duration,
    });
    if (duration > 0) {
      return new Promise<void>((resolve) => {
        setTimeout(() => {
          resolve();
        }, duration);
      });
    }
  }

  /**
   * 弹出确认窗口
   */
  static modal(text, title = '提示') {
    return new Promise((resolve, reject) => {
      Taro.showModal({
        title: title,
        content: text,
        showCancel: false,
        success: (res) => {
          resolve(res);
        },
        fail: (res) => {
          reject(res);
        },
      });
    });
  }

  /**
   * 弹出确认窗口
   */
  static confirm(text: string, payload = {}, title = '提示') {
    return new Promise((resolve, reject) => {
      Taro.showModal({
        title: title,
        content: text,
        showCancel: true,
        success: (res) => {
          if (res.confirm) {
            resolve(payload);
          } else if (res.cancel) {
            reject(payload);
          }
        },
        fail: () => {
          reject(payload);
        },
      });
    });
  }

  static toast(title: string, duration = 1000) {
    Taro.showToast({
      title: title,
      icon: 'none',
      mask: true,
      duration: duration,
    });
    if (duration > 0) {
      return new Promise<void>((resolve) => {
        setTimeout(() => {
          resolve();
        }, duration);
      });
    }
  }
}
